package ui.activity.home;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import net.ck.hzq.NewsWebViewActivity;
import net.ck.hzq.R;

import java.util.List;

import javax.inject.Inject;

import base.BaseAct;
import butterknife.BindView;
import cn.finalteam.loadingviewfinal.HeaderAndFooterRecyclerViewAdapter;
import cn.finalteam.loadingviewfinal.OnDefaultRefreshListener;
import cn.finalteam.loadingviewfinal.OnLoadMoreListener;
import cn.finalteam.loadingviewfinal.PtrClassicFrameLayout;
import cn.finalteam.loadingviewfinal.PtrFrameLayout;
import cn.finalteam.loadingviewfinal.RecyclerViewFinal;
import dao.HomeNewsBean;
import event.NetErrorEvent;
import ui.activity.home.biz.EnterPriseassistanceBiz;
import ui.activity.home.component.DaggerEnterPriseassistanceComponent;
import ui.activity.home.contract.EnterPriseassistanceContract;
import ui.activity.home.module.EnterPriseassistanceModule;
import ui.activity.home.presenter.EnterPriseassistancePresenter;
import ui.adapter.EnterpriseassistanceAdapter;


/**
 * 企业辅助
 * Created by chenjian on 2017/8/4.
 */

public class EnterPriseAct extends BaseAct implements EnterPriseassistanceContract.View {


    @Inject
    EnterPriseassistancePresenter presenter;
    @Inject
    EnterPriseassistanceBiz biz;

    @BindView(R.id.pull_refresh_rv)
    RecyclerViewFinal rv;
    @BindView(R.id.pull_refresh_ptr)
    PtrClassicFrameLayout ptr;

    private SharedPreferences sp;

    EnterpriseassistanceAdapter adp;


    @Override
    protected void setRoot() {
        setTitleBar(R.color.colorBlue);
        setContentView(R.layout.activity_enterpriseassistance);

    }

    @Override
    protected void setup() {
        DaggerEnterPriseassistanceComponent.builder().enterPriseassistanceModule(new EnterPriseassistanceModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.enterpriseassistance), "");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        sp = getSharedPreferences("ToBuy", Context.MODE_PRIVATE);
        rv.setLayoutManager(new LinearLayoutManager(EnterPriseAct.this));
        rv.setItemAnimator(new DefaultItemAnimator());
        rv.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        ptr.setLastUpdateTimeRelateObject(this);
        ptr.disableWhenHorizontalMove(true);
        rv.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void loadMore() {
            }
        });
        ptr.setOnRefreshListener(new OnDefaultRefreshListener() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                presenter.getNewsList(true, sp.getString("id", "0"));
            }
        });
        rv.setOnItemClickListener(new HeaderAndFooterRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView.ViewHolder holder, int position) {
                NewsWebViewActivity.start(EnterPriseAct.this, adp.getData().get(position));
            }
        });
        ptr.autoRefresh(true);
    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }


    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }


    @Override
    public void upDateUI(List<HomeNewsBean> bean) {
        if (adp == null) {
            adp = new EnterpriseassistanceAdapter(EnterPriseAct.this, bean);
            rv.setAdapter(adp);
        } else {
            adp.notifyDataSetChanged();
        }
    }

    @Override
    public void refreshComplete() {
        ptr.onRefreshComplete();
    }


    @Override
    public void loadMoreComplete() {
        rv.onLoadMoreComplete();
    }

    @Override
    public void hasLoadMore(boolean b) {
        rv.setHasLoadMore(b);
    }

}
